package com.example.batch.config;

import com.example.batch.constant.JobConstant;
import com.example.batch.process.ListItemProcess;
import com.example.batch.reader.MyFlatFileReader;
import com.example.batch.task.LineProcessTasklet;
import com.example.batch.task.LineReaderTasklet;
import com.example.batch.writer.MyFlatFileItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;


@Configuration
public class TaskletConfig extends BaseConfig {

    final ListItemProcess processor;
    final MyFlatFileReader reader;
    final MyFlatFileItemWriter writer;

    @Autowired
    public TaskletConfig(ListItemProcess processor,MyFlatFileReader reader,MyFlatFileItemWriter writer) {
        this.processor = processor;
        this.reader = reader;
        this.writer = writer;
    }


    private java.lang.String filePath = "/Users/yuhang/Downloads/multi-result.1.txt";

    private ArrayBlockingQueue<List<String>> blockingQueue=new ArrayBlockingQueue<>(1000);

    @Bean
    public Step readTaskLet()  {
        return stepBuilderFactory.get("readTaskLet")
                .tasklet(new LineReaderTasklet(filePath,blockingQueue))
                .build();
    }

    @Bean
    public Step processTaskLet() {
        return stepBuilderFactory.get("processTaskLet")
                .tasklet(new LineProcessTasklet(blockingQueue))
                .taskExecutor(new SimpleAsyncTaskExecutor("spring_batch"))
                .build();
    }



    @Bean
    public Job taskLetJob()  {
        return jobBuilderFactory.get(JobConstant.TASKLET_JOB_DEMO)
                .incrementer(new RunIdIncrementer())
                .start(readTaskLet())
                .next(processTaskLet())
                .build()
                ;
    }


}
